<?php
/**
 * (logic) 库存总表（国内和海外仓库存）（非fba库存）
 */

namespace App\Logic\Inventory;

use App\Models\Inventory\AreaCn;
use App\Models\Inventory\InventoryTotalArea;
use App\Models\Inventory\PublicCn;
use App\Models\OverSeasBa\DropshippingInventoryFnsku;
use App\Models\OverSeasBa\InventoryBoxFNSku;
use App\Models\OverSeasBa\RemovaleInventoryFNSku;
use App\Models\OverSeasBa\Warehouse;
use Illuminate\Support\Facades\DB;
class SyncInventoryAreaNotFBAStock
{
    /**
     * 设定：每隔*分钟运行
     */
    public static function exe(){

        try{
            $warehouse = [];
            InventoryTotalArea::query()->orderBy('id')
                ->chunkById(500, function ($items) use (&$warehouse){
                    DB::beginTransaction();
                    foreach ($items as $_item){

                        if(!array_key_exists($_item->area_id, $warehouse)){
                            $warehouse[$_item->area_id] = Warehouse::getIdsByAreaId($_item->area_id);
                        }

                        //公共
                        $publicInstant = PublicCn::getInstant($_item->psku_id);
                        $publicTransit = PublicCn::getTransit($_item->psku_id);
                        $publicReserve = PublicCn::getReserve($_item->psku_id);

                        //国内
                        $instant = AreaCn::getInstant($_item->area_id, $_item->psku_id);
                        $transit = AreaCn::getTransit($_item->area_id, $_item->psku_id);
                        $reserve = AreaCn::getReserve($_item->area_id, $_item->psku_id);

                        //海外仓（中转仓、移库仓、一件代发仓）
                        $warehouseIds = $warehouse[$_item->area_id];
                        $inventoryBox = InventoryBoxFNSku::getInstantAndTransit($warehouseIds, $_item->psku_id);
                        $removaleInventory = RemovaleInventoryFNSku::getInstantAndTransit($warehouseIds, $_item->psku_id);
                        $dropshippingInventory = DropshippingInventoryFnsku::getInstantAndTransit($warehouseIds, $_item->psku_id);

                        //更新库存
                        $_item->public_instant = $publicInstant;
                        $_item->public_transit = $publicTransit;
                        $_item->public_reserve = $publicReserve;

                        $_item->cn_instant = $instant;
                        $_item->cn_transit = $transit;
                        $_item->cn_reserve = $reserve;

                        $_item->os_box_instant = $inventoryBox->instant ?? 0;
                        $_item->os_box_transit = $inventoryBox->transit ?? 0;
                        $_item->os_box_reserve = $inventoryBox->reserve ?? 0;

                        $_item->os_removale_instant = $removaleInventory->instant ?? 0;
                        $_item->os_removale_transit = $removaleInventory->transit ?? 0;
                        $_item->os_removale_reserve = $removaleInventory->reserve ?? 0;

                        $_item->os_dropshipping_instant = $dropshippingInventory->instant ?? 0;
                        $_item->os_dropshipping_transit = $dropshippingInventory->transit ?? 0;
                        $_item->os_dropshipping_reserve = $dropshippingInventory->reserve ?? 0;

                        $_item->os_instant = ($inventoryBox->instant ?? 0) + ($removaleInventory->instant ?? 0) + ($dropshippingInventory->instant ?? 0);
                        $_item->os_transit = ($inventoryBox->transit ?? 0) + ($removaleInventory->transit ?? 0) + ($dropshippingInventory->transit ?? 0);
                        $_item->os_reserve = ($inventoryBox->reserve ?? 0) + ($removaleInventory->reserve ?? 0) + ($dropshippingInventory->reserve ?? 0);
                        $_item->save();
                    }
                    DB::commit();
                });
        }catch (\Exception $e){
            DB::rollBack();
            report($e);
        }
    }
}
